var ClubManagementWindow = Window.extend({
    _ccsFileName: res.ClubManagementWindow_json,
    _clubId: -1,
    _currentSelectTag: '',
    _viewClassItem: {
        'btn_manage_player': 'ClubMemberManagerTab',
        'btn_manage_room': 'ClubRoomManagerTab',
        'btn_manage_info': 'ClubApplyMsgTab',
        'btn_manage_rizhi': 'ClubMemberLogTab',
        'btn_clubMerge': 'ClubMergeTab',
    },

    initWindow: function (clubId, msgCount) {
        this._clubId = clubId;
        this._msgCount = msgCount;
        if (msgCount > 0) {
            this._nodeList['btn_manage_info.numbg'].setVisible(true);
            this._nodeList['btn_manage_info.numbg.num'].setString(msgCount);
        } else {
            this._nodeList['btn_manage_info.numbg'].setVisible(false);
        }

        this._btnRefreshView('btn_manage_player');
    },

    _initClickListeners: function () {
        this._clickListeners = {
            'btn_manage_player': '_clickBtnChangeView',
            'btn_manage_room': '_clickBtnChangeView',
            'btn_manage_info': '_clickBtnChangeView',
            'btn_manage_rizhi': '_clickBtnChangeView',
            'btn_clubMerge': '_clickBtnChangeView',
        }
    },

    /**
     * 监听亲友圈消息
     * @private
     */
    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubMsgCode.SetAdmin] = '_setAdminHandler';
        this._eventListeners[ClubMsgCode.UnsetAdmin] = '_setUnsetAdminHandler';
        this._eventListeners[ClubMsgCode.OnlineMember] = '_onlineMemberHandler';
        this._eventListeners[ClubMsgCode.ListClubRoom] = '_onlineListClubRoomHandler';
        this._eventListeners[ClubMsgCode.QueryMSG] = '_queryMSGHandler';
        this._eventListeners[ClubMsgCode.AgreeClub] = '_agreeClubHandler';
        this._eventListeners[ClubMsgCode.AgreeComboClub] = '_agreeComboClubHandler';
        this._eventListeners[ClubMsgCode.Feeds] = '_feedsHandler';
        this._eventListeners[ClubMsgCode.BlackListDel] = '_memberStateChangeHandler';
        this._eventListeners[ClubMsgCode.BlackListAdd] = '_memberStateChangeHandler';
        this._eventListeners[ClubMsgCode.SetAdmin] = '_memberStateChangeHandler';
        this._eventListeners[ClubMsgCode.UnsetAdmin] = '_memberStateChangeHandler';

        this._eventListeners[ClubMsgCode.AllMemberInfo] = '_allMemberInfoHandler';
        this._eventListeners[ClubMsgCode.RemoveClubMember] = '_removeClubMemberHandler';
        this._eventListeners[ClubMsgCode.AddClubGroupUids] = '_queryClubGroupHandler';
        this._eventListeners[ClubMsgCode.QueryClubGroup] = '_queryClubGroupHandler';
        this._eventListeners[ClubMsgCode.AddmodifyClubGroup] = '_queryClubGroupHandler';
    },

    /**
     * 设置管理员成功
     * @param event
     * @private
     */
    _setAdminHandler: function (data) {
        if (data.result == 0) {
            showAlert('成功提升为管理员');
            this._msgRefreshView(this._currentSelectTag);
        }
    },

    /**
     * 取消管理员成功
     * @param event
     * @private
     */
    _setUnsetAdminHandler: function (data) {
        if (data.result == 0) {
            showAlert('成功删除管理员权限');
            this._msgRefreshView(this._currentSelectTag);
        }
    },

    /**
     * 成员上线
     * @private
     */
    _onlineMemberHandler: function () {
        this._msgRefreshView(this._currentSelectTag);
    },

    /**
     * 房间列表
     * @private
     */
    _onlineListClubRoomHandler: function (data) {
        var club = ClubDataManager.getClubData(this._clubId);
        club.createRoomList = data['arr'];
        ClubDataManager.refreshClubData(this._clubId, club);
        this._msgRefreshView(this._currentSelectTag);
    },

    /**
     * 亲友圈消息
     * @param data
     * @private
     */
    _queryMSGHandler: function (data) {
        var club = ClubDataManager.getClubData(this._clubId);
        club.clubMsgs = data['arr'];
        ClubDataManager.refreshClubData(this._clubId, club);
        this._msgRefreshView(this._currentSelectTag);

        var count = 0;
        for (var i = 0; i < data['arr'].length; i++) {
            var obj = data['arr'][i];
            //有玩家申请加入亲友圈
            if (obj.type == 'apply') {
                count++;
            }
            //有人申请合并亲友圈
            if (obj['detail'] && obj['detail']['cmd'] == ClubMsgCode.ApplyComboClub && obj['detail']['club_id'] == this._clubId) {
                count++;
            }
        }
        if (count == 0) {
            this._nodeList['btn_manage_info.numbg'].setVisible(false);
        } else {
            this._nodeList['btn_manage_info.numbg.num'].setString(count);
        }

    },

    _agreeClubHandler: function (data) {
        if (data.result == 0) {
            showAlert('操作成功');
        }
        network.send(2103, {cmd: 'queryMSG', club_id: this._clubId});
    },

    _agreeComboClubHandler: function (data) {
        if (data.result == 0) {
            if (data.msg == '') {
                showAlert('您已拒绝合群请求!');
            } else {
                showAlert(data.msg);
            }
        }
        network.send(2103, {cmd: 'queryMSG', club_id: this.club_id});
    },

    _feedsHandler: function (data) {
        var club = ClubDataManager.getClubData(this._clubId);
        var logArr = [];
        var selectCMD = ['removeClubMember', 'leaveClub', 'agreeClub', 'addClubMember'];//筛选的日志
        for (var i = 0; i < data.arr.length; i++) {
            for (var j = 0; j < selectCMD.length; j++) {
                if (selectCMD[j] == data.arr[i].data.cmd) {
                    logArr.push(data.arr[i]);
                }
            }
        }
        club.clubLog = logArr;
        ClubDataManager.refreshClubData(this._clubId, club);
        this._msgRefreshView(this._currentSelectTag);
    },

    _memberStateChangeHandler:function(data){
        this._msgRefreshView(this._currentSelectTag);
    },


    /**
     * 数据
     * @private
     */
    _allMemberInfoHandler: function (data) {
        this._msgRefreshView(this._currentSelectTag);
    },
    /**
     * 删除成员回调
     * @private
     */
    _removeClubMemberHandler: function (data) {
        if (data['msg']) {
            showAlert(data['msg']);
        }
        network.send(2103, {cmd: ClubMsgCode.AllMemberInfo, club_id: this._clubId, op: 2});
    },
    /**
     * 分组信息请求回调
     * @param data
     * @private
     */
    _queryClubGroupHandler: function (data) {
        // if (data['result'] != 0 && data['msg']) {
        //     showAlert(data['msg']);
        //     return;
        // }
        // var groups = data.info['groups']['groups'] || data.info['groups'];
        // ClubDataManager.clubGroupsRefresh(this._clubId, groups);
        this._msgRefreshView(this._currentSelectTag);
    },

    /**
     * 点击左侧页签 切换显示
     * @private
     */
    _clickBtnChangeView: function (targer) {
        if (this._currentSelectTag == targer.getName()) {
            return;
        }
        this._btnRefreshView(targer.getName(), true);
    },

    /**
     * 通过消息来刷新显示
     * @private
     */
    _msgRefreshView: function () {
        hideLoading();
        var selectLayerName = this._viewClassItem[this._currentSelectTag];
        var itemLayerClass = this[selectLayerName];
        if (itemLayerClass && itemLayerClass['refreshView']) {
            itemLayerClass.refreshView(this._clubId);
        }
    },

    /**
     * 通过按钮切换刷新界面
     * @param selectName
     * @private
     */
    _btnRefreshView: function (selectName) {
        var selectLayerName = this._viewClassItem[selectName];
        this._currentSelectTag = selectName;
        var btnList = _.keys(this._viewClassItem);
        for (var i = 0; i < btnList.length; i++) {
            var btnName = btnList[i];
            var isSelect = btnName == selectName;
            this._nodeList[btnName + '.selectBtn'].setVisible(isSelect);
            this._nodeList[btnName + '.selectLabel'].setVisible(isSelect);

            //根据按钮名称 找到 对应的页签类名,如果存在已经创建的item 直接设置成flase
            var itemClass = this._viewClassItem[btnName];
            if (this[itemClass]) {
                this[itemClass].setVisible(false);
            }
        }

        var itemLayerClass = this[selectLayerName];
        if (itemLayerClass == undefined) {
            this[selectLayerName] = addItem(selectLayerName, this._nodeList['viewNode'], this._clubId);
            itemLayerClass = this[selectLayerName];
        }

        if (itemLayerClass && itemLayerClass['sendMsg']) {
            showLoading('加载中...');
            itemLayerClass.sendMsg();
        }
        itemLayerClass.setVisible(true);
    },
});